package com.taijihuabao.modules.service.impl;

import com.github.pagehelper.PageHelper;
import com.taijihuabao.common.excel.ExcelUtil;
import com.taijihuabao.common.commonutils.PageResult;
import com.taijihuabao.common.redisconfig.UserLoad;
import com.taijihuabao.modules.entity.ChaimDetailEntity;
import com.taijihuabao.modules.entity.ChaimEntity;
import com.taijihuabao.modules.entity.ExpenseEntity;
import com.taijihuabao.modules.mapper.ChaimDetailMapper;
import com.taijihuabao.modules.mapper.ChaimMapper;
import com.taijihuabao.modules.mapper.ExpenseMapper;
import com.taijihuabao.modules.model.basedto.PageDTO;
import com.taijihuabao.modules.model.chaim.dto.ChaimInsertDTO;
import com.taijihuabao.modules.model.chaim.vo.ChaimExportVo;
import com.taijihuabao.modules.model.chaim.vo.ChaimHtmlVo;
import com.taijihuabao.modules.service.ChaimService;
import com.taijihuabao.modules.service.ExpenseFlowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.UUID;

/**
 *报销
 * @author xuejingtai
 */
@Service
public class ChaimServiceImpl implements ChaimService {

    @Autowired
    private ChaimMapper chaimMapper;
    @Autowired
    private ChaimDetailMapper chaimDetailMapper;
    @Autowired
    private ExpenseFlowService expenseFlowService;
    @Autowired
    private ExpenseMapper expenseMapper;
    @Override
    public void insertChaim(ChaimInsertDTO chaimInsertDTO) {
        ChaimEntity chaimEntity = new ChaimEntity();
        chaimEntity.setUId(UserLoad.getUser().getId());
        chaimEntity.setChaimCode(chaimInsertDTO.getChaimCode());
        chaimEntity.setChaimDate(chaimInsertDTO.getChaimDate());
        chaimEntity.setDeptName(chaimInsertDTO.getDeptName());
        chaimEntity.setChaimPeople(chaimInsertDTO.getChaimPeople());
        chaimEntity.setChaimMoney(chaimInsertDTO.getChaimMoney());
        chaimEntity.setChaimStatus(0);
        chaimEntity.setChaimComment(chaimInsertDTO.getChaimComment());
        chaimMapper.insertChaim(chaimEntity);
        String chaimCode = chaimEntity.getChaimCode();
        ExpenseEntity expenseEntity = new ExpenseEntity();
        expenseEntity.setExpenseId(chaimCode);
        expenseEntity.setApplyName(chaimEntity.getChaimPeople());
        expenseEntity.setApplyMoney(chaimEntity.getChaimMoney());
        expenseFlowService.initApply(expenseEntity);
    }

    @Override
    public void insertChaimDetail(ChaimDetailEntity chaimDetailEntity) {
        chaimDetailMapper.insertChaimDetail(chaimDetailEntity);
    }

    @Override
    public PageResult selectChaimEntity(PageDTO pageDTO) {
        PageHelper.startPage(pageDTO.getPage(), pageDTO.getSize());
        List<ChaimEntity> list = chaimMapper.selectChaimEntity(UserLoad.getUser().getId());
        return new PageResult(list);
    }


    @Override
    public void exportExcel() {
        List<ChaimExportVo> list = chaimMapper.selectInformation();
        ExcelUtil.exportExcel(list);
    }

    @Override
    public ChaimHtmlVo selectById(Long id) {
        ChaimEntity chaimEntity = chaimMapper.selectById(id);
        List<ChaimDetailEntity> list = chaimDetailMapper.selectByChaimCode(chaimEntity.getChaimCode());
        ChaimHtmlVo chaimHtmlVo = new ChaimHtmlVo();
        chaimHtmlVo.setChaimEntity(chaimEntity);
        chaimHtmlVo.setList(list);
        return chaimHtmlVo;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void deleteById(String chaimCode) {
        chaimMapper.deleteById(chaimCode);
        chaimDetailMapper.deleteDetail(chaimCode);
    }

    @Override
    public void deleteByChaimCode(String chaimCode) {
        chaimDetailMapper.deleteDetail(chaimCode);
    }

    public static String getUUID_16() {
        int machineId = 1;
        int hashCodeV = UUID.randomUUID().toString().hashCode();
        // 有可能是负数
        if (hashCodeV < 0) {
            hashCodeV = -hashCodeV;
        }
        String string = machineId + String.format("%015d", hashCodeV);
        return string;

    }
}
